/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sim.DAL;

import java.io.Serializable;
import java.sql.ResultSet;
//import java.util.Date;

/**
 *
 * @author rafik
 */

public class UserLogin implements Serializable {
   
    private Integer id;
    private String userName;
    private String password;
    private String lastLogin;
    private int numberLogins;
    private int profileId;

    //Constructeur par defaut.
    public UserLogin() {
    }

    // Pourquoi utilise-ton un constructure avec un id ?
    public UserLogin(Integer id) {
        String query = "SELECT * FROM UserLogin u WHERE u.id = '"+id+"'";
        try {
            DBConnection db = new DBConnection();
            ResultSet rs = db.executeSql(query);

            while (rs.next()) {
            this.id = id;
            this.userName = rs.getString("username");
            this.password = rs.getString("password");
            this.lastLogin = rs.getString("lastLogin");
            this.numberLogins = Integer.parseInt(rs.getString("numberLogins"));
            this.profileId = Integer.parseInt(rs.getString("profileId"));
            }
        } catch(Exception exp){
            System.err.println(exp.getMessage());
        }
    }

    // Constructure avec le userName
    public UserLogin(String userName) {
        String query = "SELECT * FROM UserLogin u WHERE u.username = '"+userName+"' ";
        try {
            DBConnection db = new DBConnection();

            ResultSet rs = db.executeSql(query);

            while (rs.next()) {
            this.userName = rs.getString("username");
            this.password = rs.getString("password");
            this.lastLogin = rs.getString("last_login");
            this.numberLogins = Integer.parseInt(rs.getString("number_logins"));
            this.profileId = Integer.parseInt(rs.getString("profile_id"));
            }
            System.out.println(this.userName + " " + this.password);
        } catch(Exception exp){
            System.err.println("Exception: " + exp + " SELECT * FROM UserLogin u WHERE u.username = '"+ userName+"'");
        }
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return userName;
    }

    public void setUsername(String username) {
        this.userName = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getLastLogin() {
        return lastLogin;
    }

    public void setLastLogin(String lastLogin) {
        this.lastLogin = lastLogin;
    }

    public int getNumberLogins() {
        return numberLogins;
    }

    public void setNumberLogins(int numberLogins) {
        this.numberLogins = numberLogins;
    }

    public int getProfileId() {
        return profileId;
    }

    public void setProfileId(int profileId) {
        this.profileId = profileId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof UserLogin)) {
            return false;
        }
        UserLogin other = (UserLogin) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "sim.DAL.UserLogin[id=" + id + "]";
    }

}
